package com.my.netty;

import com.my.netty.json.DataSynClientHandler;
import io.netty.channel.Channel;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

public class NettyChannelPoolHandler implements ChannelPoolHandler {

    @Override
    public void channelReleased(Channel channel) throws Exception {
        //System.out.println("channelReleased. Channel ID: " + channel.id());
    }

    @Override
    public void channelAcquired(Channel channel) throws Exception {
        //System.out.println("channelAcquired. Channel ID: " + channel.id());
    }

    @Override
    public void channelCreated(Channel channel) throws Exception {
        SocketChannel sk = (SocketChannel) channel;
        sk.config().setKeepAlive(true);
        sk.config().setTcpNoDelay(true);
        channel.pipeline().addLast(new LineBasedFrameDecoder(1024*1024*10));
        channel.pipeline().addLast("decoder", new StringDecoder());
        channel.pipeline().addLast("encoder", new StringEncoder());
        channel.pipeline().addLast(new DataSynClientHandler());
    }
}
